package org.springboot.szsrcb.srvice;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springboot.szsrcb.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

@Service
public class RTX_Service {
	 private static final Logger logger = LoggerFactory.getLogger(RTX_Service.class);
	 
	 public   boolean  send_message(String msg,String receiver_name)  {  
            String sendImg  =   " /SendNotify.cgi? " ;  //  RTX发送消息接口   
            String host  =   "192.168.220.159" ;  //  RTX服务器地址   
            String getSessionkey  =   "/getsessionkey.cgi? " ;  //  RTX获取会话接口   
             int  port  =   8012 ;  //  RTX服务器监听端口   
            String[] receiverss  =  {receiver_name};  //  接收人，RTX帐号   
            String sender  =   "testmngt" ;  //  发送人   
            String content  =  msg ;  //  内容   
            StringBuffer sendMsgParams  =   new  StringBuffer(sendImg);  
            StringBuffer receiveUrlStr  =   new  StringBuffer();  
            try {
	             for  ( int  i  =   0 ; i  <  receiverss.length;  ++ i) {  
	                 if  (receiveUrlStr.length()  ==   0 ) {  
	                    receiveUrlStr.append(receiverss[i]);  
	                }  else  {  
	                    receiveUrlStr.append( ","   +  receiverss[i]);  
	                }  
	            }  
	            sendMsgParams.append( "&receiver="   +  receiveUrlStr);  
	             if  (content  !=   null ){  
	                sendMsgParams.append( "&msg="   +   new  String(content.getBytes( "utf-8" ),  "utf-8" ));  
	            }  
	      
	             if  (sender  !=   null ) {  
	                sendMsgParams.append( "&sender="   +  sender);  
	            }  
	            URL url;
				
					url = new  URL( "HTTP" , host, port, sendMsgParams.toString());
				
	            HttpURLConnection httpconn  =  (HttpURLConnection) url.openConnection();  
	            String ret  =  httpconn.getHeaderField( 3 );
	            return true;  
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}  
        }   
}
